<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Manage Equipment Page</title>
<link href="css/basic_form_style.css" rel="stylesheet" type="text/css" />

<!--start autocomplete support-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script src="jquery/dudi/jquery.autocomplete.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/autocomplete_styles.css" type="text/css" media="screen" />
<script type="text/javascript">
 //<![CDATA[
 var a1;
 jQuery(function() {
 var options = {
 serviceUrl: 'content/autocomplete_equipment.php',
 width: 300,
 delimiter: /(,|;)\s*/,
 deferRequestBy: 0, //miliseconds
 params: { country: 'Yes' },
 noCache: false //set to true, to disable caching
 };
 a1 = $('#query').autocomplete(options);

 $('#navigation a').each(function() {
 $(this).click(function(e) {
 var element = $(this).attr('href');
 $('html').animate({ scrollTop: $(element).offset().top }, 300, null, function() { document.location = element; });
 e.preventDefault();
 });
 });

 });

//]]>
</script>
<style type="text/css">
	.autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:6px 0 0 6px; /* IE6 fix: */ _background:none; _margin:1px 0 0 0; }
	.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px;  _margin:0; _overflow-x:hidden; }
	.autocomplete .selected { background:#F0F0F0; }
	.autocomplete div { padding:2px 5px; white-space:nowrap; overflow:hidden; }
	.autocomplete strong { font-weight:normal; color:#3399FF; }
</style>
<!--end autocomplete support-->
    
<script type="text/javascript">
function changeName(newname)
{
	alert("Item "+newname+" was successfuly selected. Fill other details to order it");
	document.forms.order.textFind.value = newname;
}

/*getting txtField in the format "2012-6-10 00:00:00" and return Date object
* updating the value of txt field to TIMESTAMP format.
*/
function convertToDate(txtField)
{
	//var strDate = "2012-6-10 00:00:00";
	//var strDate = "29-6-2012 13:13:21";
	var strDate = txtField.value;
	var begin=0;
	var end =strDate.indexOf('-',begin+1);
	var strDay= parseInt(strDate.substring(begin, end));
	begin = end+1;
	end =strDate.indexOf('-',begin);
	var strMonth= parseInt(strDate.substring(begin, end));
	begin = end+1;
	end =strDate.indexOf(' ',begin);
	var strYear= parseInt(strDate.substring(begin, end));	
	begin = end+1;
	end =strDate.indexOf(':',begin);
	var strHouer= parseInt(strDate.substring(begin, end));
	begin = end+1;
	end =strDate.indexOf(':',begin);
	var strMin= parseInt(strDate.substring(begin, end));
	begin = end+1;
	var strSec = parseInt(strDate.substring(begin, strDate.length));
	
	var ansDate = new Date(strYear,strMonth-1, strDay, strHouer, strMin,strSec);
	//updating the value of txt field to TIMESTAMP format.
	txtField.value = strYear+"-"+strMonth+"-"+strDay+" "+strHouer+":"+strMin+":"+strSec;
	//alert(txtField.value);
	return ansDate;
}

//compare start_date to end_date: return -1 if start date is after end date.
function compareDates()
{
	var sDate = convertToDate(document.forms.order.startDate);
	var eDate = convertToDate(document.forms.order.endDate);
	if(sDate==eDate)
		return 0;
	if(sDate<eDate)
		return 1;
	if(sDate>eDate)		
		return -1;
}

//check validity of order form fields. On success, submit the form.
function checkOrder()
{
	alert("checking..");
	var f = document.forms.order;

	if(f.textFind.value.length<1){
		alert("Select or enter an equipment name");
		return;
	}
	if(f.textID.value.length<1){
		alert("Enter your full ID number - with control digit");
		return;
	}
	if(compareDates()!=1){
		alert ("Indvalid Dates: start date must be before end date. please choose sensible dates.");
		convertToDate(document.forms.order.startDate);
		convertToDate(document.forms.order.endDate);
		return;
	}	
	
	f.submit();
}
</script>

<script language="JavaScript" src="content/datepick/ts_picker.js">
//Script by Denis Gritcyuk: tspicker@yahoo.com
//Submitted to JavaScript Kit (http://javascriptkit.com)
//Visit http://javascriptkit.com for this script
</script>

</head>
<body>
<script language="JavaScript">	

</script>
<?php
//Rquire MySqlAdapter.
require_once(dirname(__FILE__).'/../db/MySqlAdapter.php');

/*	connect to DB and create new if not exist
*	Step 1: open a connection to the DB
*/
$conn = MySqlAdapter::connect1();
if ($conn):
	$result = MySqlAdapter::connect2();
else:
	die ("Could not connect to db " . mysql_error());
endif;

if($result)
{
	//Step 2: Select a database (schema)
	$result = MySqlAdapter::connect2()
	or die ("Could not select the schema 'ohel_shelomo_2012': " . mysql_error());
}
?>

<center>
<h1  class="primeHead">Manage Equipment</h1>
<table width="950" border="1" cellpadding="1" bordercolor="#FFFFFF">
  <tr>
    <td width="12%" class="subHead">Search: </td>
    <td width="88%"><form action="index.php?s=19" method="post" name="find" target="_self" lang="en">
    <input name="textFind" id="query" type="text" value="what to search" size="30" /> <input name="submitFind" type="submit" value="submit"/></form></td>
  </tr>
<!--
  <tr>
    <td>Order: </td>
    <td>
    
    <form action="index.php?s=18" method="post" name="order" target="_self" dir="ltr">
<table width="98%" border="1" cellpadding="0">
  <tr>
    <th width="22%" scope="row">equipment name: </th>
    <td width="78%"><input name="textFind" id="textFind" type="text" value="" size="30" /></td>
  </tr>
  <tr>
    <th scope="row">pupil ID:</th>
    <td><input name="textID" type="text" value="" size="11" maxlength="10" /></td>
  </tr>
  <tr>
    <th height="48" scope="row">Start Date:</th>
    <td>Date <input name="startDate" type="text" />
		<a href="javascript:show_calendar('document.order.startDate', document.order.startDate.value);"><img src="content/datepick/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the startDate"></a>
	</td>
  </tr>
  <tr>
    <th height="55" scope="row">End Date:</th>
    <td>Date <input name="endDate" type="text" />
		<a href="javascript:show_calendar('document.order.endDate', document.order.endDate.value);"><img src="content/datepick/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the startDate"></a>
	</td>
  </tr>
  <tr>
    <th height="122" scope="row">&nbsp;</th>
    <td><input name="submitOrder" type="button" value="submit" onClick="checkOrder();"/><input name="clearOrder" type="reset" value="clear" /></td>
  </tr>
</table>
</form>
     
    </td>
  </tr>
-->  
</table>

<?php
if( isset($_POST['textFind']) && $_POST['textFind']!="what to search" && $_POST['textFind']!="" )
{	

	//echo "<br>select * from equipment where name LIKE '%".$_POST['textFind']."%'<br>";
	$query = "select * from equipment where name LIKE '%".$_POST['textFind']."%'";
	$result = mysql_query($query) or die ("Invalid insert " . mysql_error());

	echo "<br>Select an item from the Search Results:<br><br>";	
	?>

	<table border=1>
		<tr><th>catalogic number111</th>
		<th> name</th>
		<th> description</th>
		</tr>
	<?php 

	while ($row = mysql_fetch_array($result))
	{
		echo "<tr>";
		echo '<td><a href="index.php?s=19&eqcnumber='.$row['id'].'" >'.$row['id'].'</a></td>';
		echo "<td>".$row['name']."</td>";
		echo "<td>".$row['description']."</td>";
		echo "</tr>";
	}	
	echo "</table>";

}else if( isset($_GET['eqcnumber']) && $_GET['eqcnumber']!="" ){
//an item was selected.Show all orders for it:		
	//echo "<br>select * from equipment where equipmentID = ".$_GET['eqcnumber']."%'<br>";
	$query = "select * from reservation where reservation.equipmentID = ".$_GET['eqcnumber'];
	$result = mysql_query($query) or die ("Invalid insert " . mysql_error());

	//echo "<br>Select: "; //an Order from the Search Results:
	echo "<br><br>";
	?>

	<table border=1 id="ordersTable">
		<tr><th colspan="7">Select an Order from the Search Results:</th></tr>
		<tr>      
		<th>Catalogic Order</th>
		<th>Equipment ID</th>
		<th>Ordered By ID</th>
		<th>From Date</th>
		<th>until Date</th>
		<th>was taken ?</th>
		<th>was returned ?</th>        
		</tr>
	<?php 

	while ($row = mysql_fetch_array($result))
	{		
		echo "<tr>";
		echo '<td><a href="#" onClick="showReservation('.$row['equipmentID'].', '.$row['userID'].',\''.$row['lentDate'].'\', \''.$row['returnDate'].'\', '.$row['wastaken'].', '.$row['wasreturned'].', '.$row['id'].');" >'.$row['id'].'</a></td>';
		echo "<th>".$row['equipmentID']."</th>";
		echo "<th>".$row['userID']."</th>";
		echo "<th>".$row['lentDate']."</th>";
		echo "<th>".$row['returnDate']."</th>";
		echo "<th>".$row['wastaken']."</th>";
		echo "<th>".$row['wasreturned']."</th>";
		echo "</tr>";
	}	
	echo "</table>";
?>

<div id="takereturn">
<h1 id="takereturndiv1"></h1>
	<form name="takereturnform" action="index.php?s=20" method="post">
    	<select name="takeorreturn" size="1" dir="ltr" lang="en">
        	<option value="wastaken">takes</option>
        	<option value="wasreturned">returnes</option>
        </select>        
<h1 id="takereturndiv2"></h1>    
		<input type="hidden" name="orderid">
		<input name="" type="submit" value="OK">
    </form>
</div>

<script type="text/javascript">
document.getElementById("takereturn").style.visibility='hidden';

/*
*This function change page visibility after an order was choosed from the orders table:
*/
function showReservation(equipmentID, userID ,lentDate, returnDate, wastaken, wasreturned, id){
	document.getElementById("ordersTable").style.visibility='hidden';	
	var str1 = "Student ID "+userID;
	var str2 = "the item ID "+equipmentID+" of slot time: start-date: "+lentDate+" return-date: "+returnDate;
	document.getElementById("takereturndiv1").innerHTML = str1;
	document.getElementById("takereturndiv2").innerHTML = str2;
	document.forms.takereturnform.orderid.value = id;
	document.getElementById("takereturn").style.visibility='visible';
}
</script>
<?php	

	}else if( isset($_GET['ordercnumber']) && $_GET['ordercnumber']!="" ){	
			echo "dudi successed";
			
		}
?>

<br />
<img src="image/order.jpg" width="175" height="170" alt="orderEquipment" />
</center>

<?php
	//close connection with the Data Base
	mysql_close($conn);
?>
</body>
</html>
